﻿#include "Global.h"
#include <QInputDialog>
#include <QGridLayout>
#include "CAlarmPoint.h"
#include <QLabel>
#include <QFile>
#include "UToolBarManager.h"
#include "UNodeManager.h"
#include "CBaseNode.h"
QSet<QString> g_allIds;  // 真正的定义
CBaseNode* g_pRoot = nullptr;               //设备管理树根节点
CBaseScene* g_pRootScene = nullptr;         //场景管理树根节点
CBaseCarouse* g_pRootCarouse = nullptr;     //轮播管理树根节点
CDeviceManager* g_pDeviceManager = nullptr; // 设备管理器
UNodeManager* g_pNodeManager = nullptr;
UToolBarManager *g_pToolBarMang;  //工具栏管理对象
QMap<QString, QString>g_mapDllName;  //驱动英文-中文
QMap<QString, QString>g_mapNameDll;  //驱动中文-英文
QMap<QString, QMap<int, SDeviceWidget>>g_mapCardWidget;



QString generateUniqueId(const QString & prefix)
{
	int counter = 1;
	QString id;
	do {
		id = QString("%1%2").arg(prefix).arg(counter++, 3, 10, QChar('0')); // 例如 n001、d002
	} while (g_allIds.contains(id));
	g_allIds.insert(id);
	return id;
}

CBaseNode * findNodeByGuid(const QString & sGuid)
{
	//QVector<CAlarmPoint*>vecAlarmPoint;
	//vecAlarmPoint=getTechAlarmPoint("A001");
	return g_pRoot->findNodeById(sGuid);
}

QVector<CAlarmPoint*> getTechAlarmPoint(QString sArea)
{
	QVector<CAlarmPoint*>vecAlarmPoint;
	getChildAlarmPoint(vecAlarmPoint, g_pRoot, 0, sArea);
	return vecAlarmPoint;
}

QVector<CAlarmPoint*> getFaultAlarmPoint(QString sArea)
{
	QVector<CAlarmPoint*>vecAlarmPoint;
	getChildAlarmPoint(vecAlarmPoint, g_pRoot, 1, sArea);
	return vecAlarmPoint;
}


bool showMessageBox(QWidget * parent, const QString & title, const QString & message, int nType, bool withCancel)
{

	QMessageBox msgBox(parent);
	msgBox.setWindowTitle(title);
	msgBox.setText(message);

	// 设置图标
	if (nType == 0)
	{
		msgBox.setWindowIcon(QIcon(":/SmartControl/Resource/info.png"));
		msgBox.setIcon(QMessageBox::Information);
	}
	else if (nType == 1)
	{
		msgBox.setWindowIcon(QIcon(":/SmartControl/Resource/alarm.png"));
		msgBox.setIcon(QMessageBox::Warning);
	}

	// 设置按钮
	if (withCancel)
		msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
	else
		msgBox.setStandardButtons(QMessageBox::Ok);

	// 改中文按钮
	msgBox.button(QMessageBox::Ok)->setText(A2T("确认"));
	if (withCancel)
		msgBox.button(QMessageBox::Cancel)->setText(A2T("关闭"));

	// 设置样式
	msgBox.setStyleSheet(R"(
            QMessageBox {
                background-color: #f0f6fc;
                font: 9pt "阿里巴巴普惠体 L";
            }
            QLabel {
                color: #003366;
            }
            QPushButton {
                background-color: #007acc;
                color: white;
                border: none;
                padding: 6px 12px;
                min-width: 80px;
                border-radius: 4px;
            }
            QPushButton:hover {
                background-color: #005a9e;
            }
        )");

	auto result = msgBox.exec();
	return result == QMessageBox::Ok;


	/*QMessageBox msgBox(parent);
	msgBox.setWindowTitle(title);
	msgBox.setText(message);

	if (nType == 0)
	{
		msgBox.setWindowIcon(QIcon(":/SmartControl/Resource/info.png"));  // 替换成你的图标路径
		msgBox.setIcon(QMessageBox::Information);
	}
		
	else if (nType == 1)
	{
		msgBox.setIcon(QMessageBox::Warning);
		msgBox.setWindowIcon(QIcon(":/SmartControl/Resource/alarm.png"));  // 替换成你的图标路径
	}	
	if (withCancel)
		msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
	else
		msgBox.setStandardButtons(QMessageBox::Ok);
	// 设置样式（蓝白风格）
	msgBox.setStyleSheet(R"(
    QMessageBox {
        background-color: #f0f6fc;
       font: 9pt "阿里巴巴普惠体 L";
    }
    QLabel {
        color: #003366;
    }
    QPushButton {
        background-color: #007acc;
        color: white;
        border: none;
        padding: 6px 12px;
        min-width: 80px;
        border-radius: 4px;
    }
    QPushButton:hover {
        background-color: #005a9e;
    }
)");
	auto result = msgBox.exec();
	return result == QMessageBox::Ok;  */

}

QString showGetText(QWidget * parent, const QString & title, const QString & label, const QString & defaultText)
{
	QInputDialog inputDialog(parent);
	inputDialog.setWindowTitle(title);
	inputDialog.setLabelText(label);
	inputDialog.setTextValue(defaultText);
	inputDialog.setInputMode(QInputDialog::TextInput);
	inputDialog.resize(400, 150);  // 可选，调大一点更美观
	// 设置图标（窗口图标）
	inputDialog.setWindowIcon(QIcon(":/SmartControl/Resource/rob.png"));  // 替换成你的图标路径
	// 设置样式（蓝白）
	inputDialog.setStyleSheet(R"(
        QDialog {
            background-color: #f0f6fc;
            font: 9pt "阿里巴巴普惠体 L";
        }
        QLabel {
            color: #003366;
        }
        QLineEdit {
            background-color: white;
            border: 1px solid #007acc;
            border-radius: 4px;
            padding: 4px;
        }
 
    )");
	// 执行并判断结果
	if (inputDialog.exec() == QDialog::Accepted) {
		QString text = inputDialog.textValue().trimmed();
		if (!text.isEmpty()) {
			return text;
		}
	}
	return QString();  // 用户取消或输入为空
}

QString getCardQss()
{
	QString sReStr = "";
	QFile fileCard(":/SmartControl/Qss/card.qss"); // 资源路径
	if (fileCard.open(QFile::ReadOnly))
	{
		sReStr = fileCard.readAll();
		fileCard.close();
	}
	return sReStr;
}

void getChildAlarmPoint(QVector<CAlarmPoint*>& vecPoint, CBaseNode* pChild, int nType, QString sArea)
{
	if (pChild->getType() == CBaseNode::enDevice)
	{
		QString sAlarmArea = "";
		//工艺报警
		if (nType == 0)
		{
			sAlarmArea = pChild->getTechAlarmArea();
		}
		else
		{
			sAlarmArea = pChild->getFaultAlarmArea();
		}
		if (sAlarmArea == sArea || sArea == "*")
		{
			QVector<CAlarmPoint*>vecAlarmPoint;
			for (int i = 0; i < pChild->getChildren().size(); i++)
			{
				CMeasurePoint* pPoint = (CMeasurePoint*)pChild->getChildren()[i];
				if (pPoint->getIsAlarmPoint())
				{
					if (nType == ((CAlarmPoint*)pPoint)->getAlarmType())
					{
						vecAlarmPoint.push_back((CAlarmPoint*)pPoint);
					}			
				}
			}
			vecPoint.append(vecAlarmPoint);
		}
	}
	else
	{
		QVector<CBaseNode*>vecChildren = pChild->getChildren();
		for (CBaseNode* pChildEx : vecChildren)
		{
			getChildAlarmPoint(vecPoint, pChildEx, nType, sArea);
		}
	}
}


Global::Global()
{
}

Global::~Global()
{
}
